<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DingTalkDept extends BaseModel
{
    use HasFactory;
    protected $table = 'dingtalk_dept';
    protected $guarded = [];
    public function _creating(&$model)
    {
        $data['parent_id'] = (int)$model->parent_id;
        $data['name'] = $model->name;
        $dept = app("DT")->department->create($data);
        if ($dept['errcode'] == 0)
            $model['dept_id'] = $dept['result']['dept_id'];
        else
            throwError($dept['errmsg']);
    }

    public function _updating(&$model)
    {
        $data['parent_id'] = (int)$model->parent_id;
        $data['name'] = $model->name;

        $dept = app("DT")->department->update($model->dept_id, $data);
        if ($dept['errcode'] > 0)
            throwError($dept['errmsg']);
    }

    public function _deleting(&$model)
    {
        $dept = app("DT")->department->delete($model->dept_id);
        if ($dept['errcode'] > 0)
            throwError($dept['errmsg']);
    }
    //app("DT")->[app]->department->create($data);
}
